package cn.doitedu.demo.topn;

import org.apache.flink.streaming.api.CheckpointingMode;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;

public class 窗口分组topn {
    public static void main(String[] args) {

        // 构造stream api 编程的环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.enableCheckpointing(5000, CheckpointingMode.EXACTLY_ONCE);
        env.getCheckpointConfig().setCheckpointStorage("file:///d:/ckpt");
        env.setParallelism(1);

        // 构造 sql 编程环境
        StreamTableEnvironment tenv = StreamTableEnvironment.create(env);


        // 创建数据源映射表
        tenv.executeSql("CREATE TABLE t1                  (\n" +
                "  uid BIGINT,                               \n" +
                "  event_id STRING,                          \n" +
                "  properties MAP<STRING,STRING>,            \n" +
                "  action_time BIGINT,                       \n" +

                "  rt as to_timestamp_ltz(action_time,3),    \n" +
                "  watermark for rt as rt                    \n" +
                ") WITH (                                    \n" +
                "  'connector' = 'kafka',                    \n" +
                "  'topic' = 'ss-1',                         \n" +
                "  'properties.bootstrap.servers' = 'doitedu:9092',\n" +
                "  'properties.group.id' = 'doit44_g1',      \n" +
                "  'scan.startup.mode' = 'latest-offset',    \n" +
                "  'value.format' = 'json',                  \n" +
                "  'value.fields-include' = 'EXCEPT_KEY'     \n" +
                ")");

        tenv.executeSql(
                "with tmp1 as (\n" +
                "    SELECT\n" +
                "        window_start,\n" +
                "    \twindow_end,\n" +
                "    \tproperties['url'] as url,\n" +
                "    \tuid,\n" +
                "    \tcount(1) as pv \n" +
                "    FROM TABLE(\n" +
                "        TUMBLE(TABLE t1,DESCRIPTOR(rt),INTERVAL '5' MINUTE)\n" +
                "    )\n" +
                "    GROUP BY \n" +
                "        window_start,\n" +
                "    \twindow_end,\n" +
                "    \tproperties['url'],\n" +
                "    \tuid\n" +
                "),\t\n" +
                "tmp2 as (\n" +
                "    SELECT\n" +
                "        window_start,\n" +
                "    \twindow_end,\n" +
                "    \turl,\n" +
                "    \tuid,\n" +
                "    \tpv,\n" +
                "    \trow_number() over(partition by window_start,window_end,url order by pv desc ) as rn \n" +
                "    FROM tmp1 \n" +
                ")\n" +
                "\n" +
                "SELECT * FROM tmp2 WHERE rn<=2").print();


    }
}
